private ["_delcode", "_currentworld", "_oldworld", "_newworld", "_notalive","_action_params"];

_trigger = _this select 0;

_delcode = { 
	private ["_crew"];
 	diag_log Format["%1 Loaded bodies list", diag_tickTime];
 	// if (d_global_debug > 0) then { diag_log Format["%1 Loaded bodies list", diag_tickTime]; };
	sleep BodiesCleanupDelay; 	
	{
		diag_log Format["%1 Added to CleanUp: %2", diag_tickTime, typeof _x];
		// if (d_global_debug > 0) then { diag_log Format["%1 Added to CleanUp: %2", diag_tickTime, typeof _x]; };
		if (not isNull _x) then {
			if (_x isKindOf "Ship" or _x isKindOf "Air" or _x isKindOf "LandVehicle") then {
				_crew = nearestObjects [_x, ["Man"], 20];
 				_crew = _crew + crew _x;
 				deleteVehicle _x;
				{
					if (not alive _x) then {deleteVehicle _x};
 				} forEach _crew;
			} else {
				deleteVehicle _x;
			};
		};
		sleep 0.5;
	} forEach _this;
};

if (isnil "BodiesCleanupDelay") then { BodiesCleanupDelay = 600; };

_currentworld = [];
while {true} do {
	sleep 60;
	if (WithBodiesCleanup) then {
		_oldworld = _currentworld;
		_currentworld = + list _trigger;
		_newworld = _oldworld - _currentworld;
		_notalive = [];
		{
			if (not alive _x) then {
				_action_params = 0;
				if (!isnil "XfMissionGetParameters") then {
					// We avoid deleting mission objects
					_action_params = count (_x call XfMissionGetParameters);
				};
				if (_action_params <= 0) then {
					_notalive = _notalive + [_x];
				};	
			};
			sleep 0.02;
		} forEach _newworld;
		if (count _notalive > 0) then {_notalive spawn _delcode};
	};
};
